package com.stylefeng.guns.rest.modular.cinema.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DefaultCinemaServiceImpl
 * Created by zghgchao 2019/8/21 14:45
 **/
@Component
@Service(interfaceClass = CinemaServiceAPI.class,executes = 10) // executes: 线程连接数
public class DefaultCinemaServiceImpl implements CinemaServiceAPI {

    @Autowired
    private MoocCinemaTMapper moocCinemaTMapper;

    @Autowired
    private MoocBrandDictTMapper moocBrandDictTMapper;

    @Autowired
    private MoocAreaDictTMapper moocAreaDictTMapper;

    @Autowired
    private MoocHallDictTMapper moocHallDictTMapper;

    @Autowired
    private MoocFieldTMapper moocFieldTMapper;

    /**
     * 1、根据CinemaQueryVO，查询影院列表
     * @param cinemaQueryVO
     * @return
     */
    @Override
    public Page<CinemaVO> getCinemas(CinemaQueryVO cinemaQueryVO) {

        // 业务实体 集合
        List<CinemaVO> cinemaVOS = new ArrayList<>();

        Page<MoocCinemaT> page = new Page<>(cinemaQueryVO.getNowPage(),cinemaQueryVO.getPageSize());
        // 判断是否传入查询条件 -> brandId,distId,hallType是否==99
        EntityWrapper<MoocCinemaT> entityWrapper = new EntityWrapper<>();
        if (cinemaQueryVO.getBrandId() != 99){
            entityWrapper.eq("brand_id",cinemaQueryVO.getBrandId());
        }
        if (cinemaQueryVO.getDistrictId() != 99){
            entityWrapper.eq("area_id",cinemaQueryVO.getDistrictId());
        }
        if (cinemaQueryVO.getHallType() != 99){ // %#3#%
            entityWrapper.like("hall_ids","%#" + cinemaQueryVO.getHallType() + "#%");
        }

        // 将数据实体转换成业务实体
        List<MoocCinemaT> moocCinemaTS = moocCinemaTMapper.selectPage(page, entityWrapper);
        for (MoocCinemaT moocCinemaT : moocCinemaTS) {
            CinemaVO cinemaVO = new CinemaVO();
            cinemaVO.setUuid(moocCinemaT.getUuid() + "");
            cinemaVO.setCinemaName(moocCinemaT.getCinemaName());
            cinemaVO.setMinimumPrice(moocCinemaT.getMinimumPrice() + "");
            cinemaVO.setAddress(moocCinemaT.getCinemaAddress());

            cinemaVOS.add(cinemaVO);
        }

        // 根据条件，判断影院列表总数
        Integer counts = moocCinemaTMapper.selectCount(entityWrapper);

        // 组织返回值对象
        Page<CinemaVO> result = new Page<>();
        result.setRecords(cinemaVOS);
        result.setSize(cinemaVOS.size());
        result.setTotal(counts);

        return result;
    }

    /**
     * 2、根据条件获取品牌列表 【除了99外，其他的数字卫isActive】
     * @param brandId
     * @return
     */
    @Override
    public List<BrandVO> getBrands(int brandId) {

        // 业务实体集合
        List<BrandVO> brandVOS = new ArrayList<>();

        // 标识位
        boolean flag = false;
        // 判断传入的id是否存在
        MoocBrandDictT moocBrandDictT = moocBrandDictTMapper.selectById(brandId);
        // 判断brandId是否等于99
        if (brandId == 99 || moocBrandDictT == null || moocBrandDictT.getUuid() == null){
            flag = true;
        }

        // 查询所有列表
        List<MoocBrandDictT> moocBrandDictTS = moocBrandDictTMapper.selectList(null);

        // 判断flag如果为true，则将99置为isActive
        for (MoocBrandDictT brandDictT : moocBrandDictTS) {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(brandDictT.getUuid() + "");
            brandVO.setBrandName(brandDictT.getShowName());
            if (flag){ // true，将99置为isActive
                if (brandDictT.getUuid() == 99){
                    brandVO.setActive(true);
                }
            }else { // false，将非99的其他品牌id和查询的进行匹配，匹配上的置为isActive
                if (brandDictT.getUuid() == brandId){
                    brandVO.setActive(true);
                }
            }
            brandVOS.add(brandVO);
        }
        return brandVOS;
    }

    /**
     * 3、获取行政区域列表
     * @param areaId
     * @return
     */
    @Override
    public List<AreaVO> getAreas(int areaId) {
        // 业务实体集合
        List<AreaVO> areaVOS = new ArrayList<>();

        // 标识位
        boolean flag = false;
        // 判断传入的id是否存在
        MoocAreaDictT moocAreaDictT = moocAreaDictTMapper.selectById(areaId);
        // 判断brandId是否等于99
        if (areaId == 99 || moocAreaDictT == null || moocAreaDictT.getUuid() == null){
            flag = true;
        }

        // 查询所有列表
        List<MoocAreaDictT> moocAreaDictTS = moocAreaDictTMapper.selectList(null);

        // 判断flag如果为true，则将99置为isActive
        for (MoocAreaDictT areaDictT : moocAreaDictTS) {
            AreaVO areaVO = new AreaVO();
            areaVO.setAreaId(areaDictT.getUuid() + "");
            areaVO.setAreaName(areaDictT.getShowName());
            if (flag){ // true，将99置为isActive
                if (areaDictT.getUuid() == 99){
                    areaVO.setActive(true);
                }
            }else { // false，将非99的其他品牌id和查询的进行匹配，匹配上的置为isActive
                if (areaDictT.getUuid() == areaId){
                    areaVO.setActive(true);
                }
            }
            areaVOS.add(areaVO);
        }
        return areaVOS;
    }

    /**
     * 4、获取影厅类型列表
     * @param hallTypeId
     * @return
     */
    @Override
    public List<HallTypeVO> getHallTypes(int hallTypeId) {
        // 业务实体集合
        List<HallTypeVO> hallTypeVOS = new ArrayList<>();

        // 标识位
        boolean flag = false;
        // 判断传入的id是否存在
        MoocHallDictT moocHallDictT = moocHallDictTMapper.selectById(hallTypeId);
        // 判断brandId是否等于99
        if (hallTypeId == 99 || moocHallDictT == null || moocHallDictT.getUuid() == null){
            flag = true;
        }

        // 查询所有列表
        List<MoocHallDictT> moocHallDictTS = moocHallDictTMapper.selectList(null);

        // 判断flag如果为true，则将99置为isActive
        for (MoocHallDictT hallDictT : moocHallDictTS) {
            HallTypeVO hallTypeVO = new HallTypeVO();
            hallTypeVO.setHalltypeId(hallDictT.getUuid() + "");
            hallTypeVO.setHalltypeName(hallDictT.getShowName());
            if (flag){ // true，将99置为isActive
                if (hallDictT.getUuid() == 99){
                    hallTypeVO.setActive(true);
                }
            }else { // false，将非99的其他品牌id和查询的进行匹配，匹配上的置为isActive
                if (hallDictT.getUuid() == hallTypeId){
                    hallTypeVO.setActive(true);
                }
            }
            hallTypeVOS.add(hallTypeVO);
        }
        return hallTypeVOS;
    }

    /**
     * 5、根据影院编号，获取影院信息
     * @param cinemaId
     * @return
     */
    @Override
    public CinemaInfoVO getCinemaInfoByCinemaId(int cinemaId) {

        // 获取数据实体
        MoocCinemaT moocCinemaT = moocCinemaTMapper.selectById(cinemaId);

        // 将数据实体转换成业务实体
        CinemaInfoVO cinemaInfoVO = new CinemaInfoVO();

        cinemaInfoVO.setCinemaAdress(moocCinemaT.getCinemaAddress());
        cinemaInfoVO.setCinemaId(moocCinemaT.getUuid() + "");
        cinemaInfoVO.setCinemaName(moocCinemaT.getCinemaName());
        cinemaInfoVO.setCinemaPhone(moocCinemaT.getCinemaPhone());
        cinemaInfoVO.setImgUrl(moocCinemaT.getImgAddress());

        return cinemaInfoVO;
    }

    /**
     * 6、获取所有电影的信息和对应的放映场次信息，根据影院编号
     * @param cinemaId
     * @return
     */
    @Override
    public List<FilmInfoVO> getFilmInfoByCinemaId(int cinemaId) {
        List<FilmInfoVO> filmInfos = moocFieldTMapper.getFilmInfos(cinemaId);

        return filmInfos;
    }

    /**
     * 7、根据放映场次ID获取放映信息
     * @param fieldId
     * @return
     */
    @Override
    public HallInfoVO getFilmFieldInfo(int fieldId) {

        return moocFieldTMapper.getHallInfo(fieldId);
    }

    /**
     * 8、根据放映场次查询播放的电影编号，然后根据电影编号获取对应的电影信息
     * @param fieldId
     * @return
     */
    @Override
    public FilmInfoVO getFilmInfoByFieldId(int fieldId) {
        return moocFieldTMapper.getFilmInfoById(fieldId);
    }

    @Override
    public OrderQueryVO getOrderNeeds(int fieldId) {
        OrderQueryVO orderQueryVO = new OrderQueryVO();
        MoocFieldT moocFieldT = moocFieldTMapper.selectById(fieldId);

        orderQueryVO.setCinemaId(moocFieldT.getCinemaId()+"");
        orderQueryVO.setFilmPrice(moocFieldT.getPrice()+"");
        return null;
    }
}
